<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="1000"
        style="height: 90%; margin-top: 3.5%;"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                <span v-if="baTable.form.operate == 'Add'">{{ t('添加') }}</span>
                <span v-if="baTable.form.operate == 'Edit'">{{ t('编辑') }}</span>
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    :label-width="baTable.form.labelWidth + 'px'"
                >
                    <!-- <FormItem :label="t('所属分类')" type="remoteSelect" v-model="baTable.form.items!.category_id" :input-attr="{ pk: 'id', field: 'name', 'remote-url': '/system/product.Category/index' }" /> -->
                    <FormItem :label="t('HS CODE') " type="string" v-model="baTable.form.items!.hs_code" />
                    <FormItem :label="t('商品名称') " type="string" v-model="baTable.form.items!.name" />
                    
                    <el-form-item :label="t('报关要素')">
                        <div>
                            <el-input v-model="baTable.form.items!.sku_list" :rows="5" type="textarea" placeholder=" " style="width: 600px;" />
                        </div>
                        <div style="margin-left: 10px;">
                            <span class="btn" @click="generateSku">{{ t('生成') }}</span>
                        </div>
                    </el-form-item>
                    <el-form-item :label="t('')">
                        <sku-table 
                            v-if="baTable.form.items!.sku_structure.length > 0 && state.form.is_specs == 1" 
                            v-model="baTable.form.items!.sku" 
                            :specs="baTable.form.items!.sku_structure" 
                            :skuProperties="state.columns" 
                            ref="sku" 
                        />
                    </el-form-item>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div style="">
                <span class="btn" @click="baTable.toggleForm()">{{ t('取消') }}</span>
                <span class="btn btn-primary ml-12" v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('保存') }}
                </span>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, inject, reactive } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance } from 'element-plus'

import { uuid } from '/@/utils/random'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()
import skuTable from '/@/components/hehe/sku-table.vue'


const state = reactive({
    
    form: {
        is_specs: 1, //是否是多规格
        specsData: [
            // {id: 1, name: '规格', values: []},
        ], //规格
        skuData: [], //属性
    }, //表单
    specsArr: [], //数组 - 单个商品规格
    specInc: 1, //规格属性所需自增id
    specsData: [{id: 1, name: 'abc'}], //商品规格列表
    columns: [
        { name: t('购买币种'), id: "buy_currency", type: "currency", readOnly: true },
        { name: t('购买价格'), id: "buy_price", type: "text", pattern: /[^\d|\\.]|^\..+|(?<=^0)[^\\.]+|(?<!^\d+)\.|(?<=\.\d{5}).*/g},
        { name: t('销售币种'), id: "sale_currency", type: "currency", readOnly: true },
        { name: t('销售价格'), id: "sale_price", type: "text", pattern: /[^\d|\\.]|^\..+|(?<=^0)[^\\.]+|(?<!^\d+)\.|(?<=\.\d{5}).*/g},
    ],
    agency_list: {}
})

const generateSku = () => {
    // console.log(baTable.form.items!.sku_structure[1])
    // baTable.form.items!.sku_structure[1].values.push({
    //     id: uuid(), name: 'zise'
    // });
    // return;

    // state.form.specsData = [{ 
    //     name: "颜色", 
    //     values: [
    //         { id: uuid(), name: '黑色' },
    //         { id: uuid(), name: '白色' },
    //         { id: uuid(), name: '绿色' },
    //     ]
    // }, { 
    //     name: "尺码", 
    //     values: [
    //         { id: uuid(), name: 'S' },
    //         { id: uuid(), name: 'M' },
    //         { id: uuid(), name: 'L' },
    //     ]
    // }]

    // baTable.form.items!.sku_structure = state.form.specsData;
    // return;


    // 获取textarea中的内容，并按照换行符处理为数组  
    let sku_list = baTable.form.items!.sku_list.split('\n');  
    
    // 使用map方法去除每一行的空格，并排除空行  
    sku_list = sku_list.map(function(line) {  
        return line.trim();  
    }).filter(function(line) {  
        return line !== '';  
    });
    sku_list = new Set(sku_list);

    // {id: 1, name: '颜色', values: [ { id: 1, name: '黑色' } ]},

    if(baTable.form.operate == 'Add' && baTable.form.items!.sku_structure[0] == undefined){
        baTable.form.items!.sku_structure = [{ id: 1, name: t("产品规格"), values: [] }];
    }
    
    let is = false;

    sku_list.forEach(item => {

        is = false;

        baTable.form.items!.sku_structure[0].values.forEach(em => {
            if(em.name == item) {
                is = true;
            }
        })
        if(!is){
            baTable.form.items!.sku_structure[0].values.push({
                id: uuid(),
                name: item
            })
        }
        
    });

    // 删除不存在的规格
    baTable.form.items!.sku_structure[0].values.forEach((item, index) => {
        is = false;
        sku_list.forEach(em => {
            if(item.name == em) is = true;
        })
        if(!is) baTable.form.items!.sku_structure[0].values.splice(index, 1)
    })


    

    // state.form.specsData[0].values = baTable.form.items!.sku_structure[0].values

}


</script>

<style scoped lang="scss">
:deep(.el-upload-list--picture-card .el-upload-list__item){
    width: 90px;
    height: 90px;
}
:deep(.el-upload--picture-card){
    width: 90px;
    height: 90px;
}
:deep(.ba-upload-select-image){
    width: 90px;
}
:deep(.ba-upload.files .el-button [class*=el-icon]+span){
    position: relative;
    top: -3px;
}
</style>
